import java.util.ArrayList;

import junit.framework.TestCase;

/**
 * 
 */

/**
 * Instructiontest test all classes
 * implementing the InstructionInterface
 * @author Patrik
 *
 */
public class Instructiontest extends TestCase {

	/**
	 * @param name
	 */
	public Instructiontest(String name) {
		super(name);
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/* (non-Javadoc)
	 * @see junit.framework.TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}
	
	public void testAdd(){
		IntWrapper a=new IntWrapper(4),b=new IntWrapper(12);
		AddInstruction instruction=new AddInstruction(a,b);
		instruction.act();
		assertEquals(16,a.get());
	}
	
	public void testSub(){
		IntWrapper a=new IntWrapper(4),b=new IntWrapper(12);
		SubInstruction instruction=new SubInstruction(a,b);
		instruction.act();
		assertEquals(-8,a.get());
	}
	
	public void testMul(){
		IntWrapper a=new IntWrapper(4),b=new IntWrapper(12);
		MulInstruction instruction=new MulInstruction(a,b);
		instruction.act();
		assertEquals(48,a.get());
	}
	
	public void testDiv(){
		IntWrapper a=new IntWrapper(12),b=new IntWrapper(4);
		DivInstruction instruction=new DivInstruction(a,b);
		instruction.act();
		assertEquals(3,a.get());
	}
	
	public void testJmp(){
		IntWrapper a=new IntWrapper(4);
		JmpInstruction instruction=new JmpInstruction(a);
		assertEquals(new Integer(a.get()),instruction.act());
	}
	
	public void testJme(){
		IntWrapper a=new IntWrapper(4);
		IntWrapper b=new IntWrapper(8);
		IntWrapper c=new IntWrapper(8);
		JmeInstruction instruction=new JmeInstruction(a,b,c);
		assertEquals(new Integer(a.get()),instruction.act());
		instruction=new JmeInstruction(b,a,c);
		assertEquals(null,instruction.act());
	}
	
	public void testJmn(){
		IntWrapper a=new IntWrapper(4);
		IntWrapper b=new IntWrapper(8);
		IntWrapper c=new IntWrapper(8);
		JmnInstruction instruction=new JmnInstruction(a,b,c);
		assertEquals(null,instruction.act());
		instruction=new JmnInstruction(b,a,c);
		assertEquals(new Integer(b.get()),instruction.act());
	}
	
	public void testCall(){
		IntWrapper a=new IntWrapper(4);
		CallInstruction instruction=new CallInstruction(a);
		assertEquals(new Integer(a.get()),instruction.act());
	}
	
	public void testRet(){
		RetInstruction instruction=new RetInstruction();
		assertEquals(true,instruction instanceof RetInstruction);
	}
	
	public void testCmp(){
		IntWrapper a=new IntWrapper(3);
		IntWrapper b=new IntWrapper(4);
		IntWrapper c=new IntWrapper(4);
		CmpInstruction instruction=new CmpInstruction(a,b,c);
		instruction.act();
		assertEquals(0,a.get());
		b.set(3);
		instruction=new CmpInstruction(a,b,c);
		instruction.act();
		assertEquals(-1,a.get());
		b.set(5);
		instruction=new CmpInstruction(a,b,c);
		instruction.act();
		assertEquals(1,a.get());
	}
	
	public void testInOut(){
		IntWrapper a=new IntWrapper(4);
		IntWrapper b=new IntWrapper(6);
		InOutInstruction instruction=new InOutInstruction(a,b);
		instruction.act();
		assertEquals(6,a.get());
	}

	public void testInfo(){
		@SuppressWarnings("unused")
		GameMap map=new GameMap(100,2);
		GameMap.setPosition(30,40,-1);
		IntWrapper a=new IntWrapper(1);
		IntWrapper b=new IntWrapper(1);
		IntWrapper c=new IntWrapper(1);
		IntWrapper d=new IntWrapper(1);
		ArrayList<IntWrapper> parameters=new ArrayList<IntWrapper>();
		parameters.add(a);
		parameters.add(b);
		InfoInstruction instruction=new InfoInstruction(parameters);
		instruction.act();
		assertEquals(102,a.get());
		a.set(2);
		parameters.add(c);
		parameters.set(0,a);
		instruction=new InfoInstruction(parameters);
		instruction.act();
		assertEquals(30,a.get());
		assertEquals(40,b.get());
		a.set(3);
		parameters.add(d);
		instruction=new InfoInstruction(parameters);
		instruction.act();
		assertEquals(0,a.get());
		a.set(3);
		b.set(30);
		c.set(40);
		instruction=new InfoInstruction(parameters);
		instruction.act();
		assertEquals(-1,a.get());
		
	}

}
